<template>
  <div class="oldVipwrap">
    <nav-bar :title="cateTitle">
      <!-- <img src="/img/47.png" slot="screen" class="nav-right" /> -->
    </nav-bar>

    <div class="main_body">
      <!-- 公告 -->
      <router-link class="public_message" :to="'/public_message?article_id='+newestMessage.old.article_id" v-if="newestMessage&&newestMessage.old">
        <i class="icon"></i>
        <h4 class="title">{{newestMessage.old.title}}</h4>
      </router-link>
      <!-- 公告 -->
      <div class="special-vip" v-if="old_vip_private.length>0">
        <h3>专属物品</h3>
        
        <!-- 缺少一张背景图 TODO -->
        <router-link class="check-all" to="/privateGoods">
          <span>查看全部</span>
          <img src="/img/44.png" alt />
        </router-link>

        <div class="switch-pro">
          <div class="switch-img">
            <div class="product-info" v-for="(item, index) in old_vip_private" :key="index">
              <span class="tag">HOT</span>
              <img src="/img/61.png" alt />
              <div class="product-amount">
                <div class="product-name">
                  <h5 class="name">进项大青蛙</h5>
                  <span class="weight">{{item.weight}}g</span>
                </div>
                <van-stepper v-model="productValue[item.goods_id]" :min="0" :max="2"/>
              </div>
            </div>          
          </div>
          <!-- <div class="switch-num">
            <div class="product-amount" v-for="(item, index) in old_vip_private" :key="index">
              <p class="product-name">进项大青蛙 900g</p>
              <van-stepper v-model="productValue[item.goods_id]" min="0" max="2"/>
            </div>
          </div> -->
        </div>
      </div>

      <div class="oldVippage" :class="old_vip_private.length==0?'mtop':''">
        <side-menu :sideMenuList="sideMenuList" v-model="slider_active"></side-menu>
        <tabs :tabsList="tabsList" class="tabs" v-model="sub_active">
          <product-item-long class="itemList" slot="tabItem" v-for="(item, index) in list" :key="index" :product="item" :isOldVip="true" v-model="productValue[item.goods_id]"></product-item-long>
        </tabs>
      </div>
      <div class="btnarea">
        <van-submit-bar
          button-text="立即领取"
          @submit="onSubmit"
        >
          <span slot="tip" v-if="address">
            当前收货地址:{{address.address}} <router-link to="/myAddressManagement?from=oldVip">修改地址</router-link>
          </span>
          <span slot="tip" v-else>          
            尚未添加收货地址， <router-link to="/myAddressManagement?from=oldVip">修改地址</router-link>
          </span>
          <div class="van-submit-bar__text">
            <span>合计：</span><span class="van-submit-bar__price"> &nbsp; {{totalWeight}}斤</span>
          </div>
        </van-submit-bar>
      </div>
    </div>
  </div>
</template>

<script>
import { Stepper } from "vant";
Vue.use(Stepper);
import { Button } from 'vant';
Vue.use(Button);
import { SubmitBar } from 'vant';
Vue.use(SubmitBar);
import navBar from "@/components/nav-bar";
import sideMenu from "@/components/side-menu";
import tabs from "@/components/tabs";
import productItemLong from "@/components/product-item-long";
export default {
  name: "cate",
  data() {
    return {
      productValue: {},
      cateTitle: "老会员专享",
      slider_active: 0,
      sub_active: 0,
      newestMessage: undefined,
      sideMenuList: [],
      page: {
        nowPage: 0
      },
      goodsListObj: {},
      list: [],
      old_vip_private: []
    };
  },
  mounted(){
    this.getPrivateGoods()
    this.getCate()
    this.getLastMessage()
  },
  computed:{
    tabsList(){
      var item = this.sideMenuList[this.slider_active]
      if(!item || !item.tmenu) return []
      var arr = []
      for (let index = 0; index < item.tmenu.length; index++) {
        const element = item.tmenu[index];
        arr.push({
          id: element.id,
          title: element.name,
        })
      }
      return arr
    },
    cateId(){
      var item = this.sideMenuList[this.slider_active]
      if(!item) return 0;
      if(!item.tmenu) {
        return item.id
      }
      var sub = item.tmenu[this.sub_active]
      if(!sub) {
        return item.id
      }
      return sub.id
    },
    address(){
      return this.$store.state.selected_address || undefined
    },
    address_id(){
      return this.address ? this.address.address_id : 0
    },
    totalWeight(){
      return (this.totalWeight_g/500).toFixed(2)
    },
    totalWeight_g(){
      let we = 0
      for (const key in this.productValue) {
        if (this.productValue.hasOwnProperty(key)) {
          const num = this.productValue[key];
          const weight = this.goodsListObj[key] ? this.goodsListObj[key].weight : 0
          we += num * weight
        }
      }
      return we
    },
    getDrawGoods(){
      let goods = []
      for (const key in this.productValue) {
        if (this.productValue.hasOwnProperty(key)) {
          const num = this.productValue[key];
          if(num > 0) {
            goods.push({
              goods_id: key,
              goods_num: num
            })
          }
        }
      }
      return goods
    }
  },
  watch:{
    cateId(){
      this.page =  {
        nowPage: 0
      },
      this.list = []
      this.getGoods()
    }
  },
  methods:{
    getCate(){
      this.$store.dispatch('goodsCate', {}).then(res=>{
        var cates = res.data.result
        var arr = []
        for (const key in cates) {
          if (cates.hasOwnProperty(key)) {
            const element = cates[key];
            arr.push({
              id: key,
              title: element.name,
              tmenu: element.tmenu
            })
          }
        }
        console.log(arr)
        this.sideMenuList = arr
        this.getGoods()
      }).catch(err => {
        console.log('err', err)
      })
    },
    getPrivateGoods(){
      var data = [{
          goods_id: 1,
          weight: 900
        },
        {
          goods_id: 2,
          weight: 800
        },
        {
          goods_id: 3,
          weight: 700
        }]
      data.forEach(element => {
        this.goodsListObj[element.goods_id] = element
        this.productValue[element.goods_id] = 0
      });
      this.old_vip_private = data
    },
    getGoods(){
      this.$store.dispatch('goodsList',{
        type: 1,
        id: this.cateId,
        page: this.page.nowPage + 1
      }).then(res=>{
        // this.old_vip_private = res.data.result.old_vip_private
        res.data.result.goods_list.forEach(element => {
          this.goodsListObj[element.goods_id] = element
        });
        this.list = this.list.concat(res.data.result.goods_list)
        this.page = res.data.result.page
      })
    },
    onSubmit(){
      if(this.getDrawGoods.length == 0) return this.$toast('请选择要领取的商品')
      this.$store.dispatch('drawGoods', {
        goods: this.getDrawGoods
      }).then(res => {
        this.getPrivateGoods()
      }).catch(e => {

      })
    },
    getLastMessage(){
      this.$store.dispatch('lastMessage').then(res=>{
        this.newestMessage = res.data.result
      }).then(e=>{

      })
    },
  },
  components: {
    navBar,
    sideMenu,
    tabs,
    productItemLong
  }
};
</script>
<style lang="less">
.oldVipwrap {
  height: 100%;
  width: 100%;
  a{
    color: inherit;
  }
  .itemList{
    width: 270px;
  }
  .main_body{
    padding-top: 50px;
    position: relative;
  }
  .special-vip {
    position: relative;
    text-align: center;
    height: 230px;
    h3 {
      padding: 10px 0px 10px;
      font-size: 18px;
      font-family: PingFangSC-Medium;
      font-weight: 500;
      color: rgba(48, 48, 48, 1);
      letter-spacing: 11px;
      margin: 0;
    }
    .check-all {
      position: absolute;
      right: 3%;
      top: 10px;
      display: flex;
      align-items: center;
      font-size: 12px;
      font-family: PingFangSC-Regular;
      font-weight: 400;
      color: rgba(153, 153, 153, 1);
      img {
        padding-left: 5px;
        width: 15px;
        height: 25px;
        vertical-align: middle;
      }
    }
    .switch-pro {
      height: 175px;
      box-sizing: border-box;
      background: url(/img/37.png) no-repeat center center;
      background-size: 100%;
      position: relative;
      margin-top: 10px;
      .switch-img {
        display: flex;
        justify-content: space-around;
        .product-info {
          position: relative;
          height: 95px;
          width: 95px;
          background: url(/img/33.png) no-repeat center center;
          background-size: 100%;
          img {
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            height: 60px;
            width: 60px;
          }
          .tag {
            position: absolute;
            left: 0px;
            top: 0px;
            font-size: 9px;
            font-family: PingFangSC-Semibold;
            font-weight: 600;
            color: rgba(48, 48, 48, 1);
            text-align: center;
            line-height: 25px;
            height: 26px;
            width: 27px;
            background: url(/img/59.png) no-repeat center center;
            background-size: 100%;
          }
          .product-amount{
            background: #FFF;
            display: flex;
            flex-direction: column;
            // height: 56px;
            margin-top: 107px;
            border-radius: 5px;
            line-height: 1;
            padding: 9px;
            font-size: 12px;
            width: 99px;
            position: relative;
            margin-left: -11px;
            .product-name{
              display: flex;
              width: 100%;
              height: 12px;
              .name{
                width: 80px;
                height: 12px;
                display: -webkit-box;
                -webkit-box-orient: vertical;
                -webkit-line-clamp: 1;
                overflow: hidden;
                margin: 0;
                padding: 0;
              }
            }
            .van-stepper{
              display: flex;
              justify-content: space-around;
              width: 80px;
              height: 20px;
              margin: 0 auto;
              margin-top: 10px;
              button,input{
                width: 20px;
                height: inherit;
              }
            }
          }
          // &:first-child{
          //   .product-amount{
          //     width: 95px;
          //   }
          // }
        }
      }
      .switch-num {
        display: flex;
        justify-content: space-between;
        position: absolute;
        left: 50%;
        bottom: 10px;
        padding: 0px 5px;
        transform: translateX(-50%);
        font-size: 12px;
        font-family: PingFangSC-Medium;
        font-weight: 500;
        color: rgba(48, 48, 48, 1);
        width: 350px;
        height: 56px;
        background: url(/img/32.png) no-repeat center center;
        background-size: 100%;
        .product-amount{
          display: flex;
          flex-direction: column;
        }
        .product-name {
          padding: 5px 0px 0px;
          margin: 0;
        }
      }
    }
  }
  .oldVippage {
    display: flex;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    position: relative;
    // padding-top: 50px;
    &.mtop{
      padding-top: 50px;
    }
    .van-tabs__wrap{
      
    }
    .tabs {
      width: 100%;
      height: 45px;
      background-color: #fff;
      .van-tabs__line {
        background-color: #fff;
      }

      .van-tab {
        font-size: 13px;
        font-family: PingFangSC-Regular;
        font-weight: 400;
        color: rgba(48, 48, 48, 1);
        &.van-tab--active {
          font-size: 13px;
          font-family: PingFangSC-Regular;
          font-weight: 400;
          color: rgba(0, 124, 157, 1);
        }
      }
    }
  }
  .van-stepper__minus {
    background: #fff;
  }
  .van-stepper__plus {
    background: #fff;
  }
  .nav-right {
    height: 20px;
    width: 20px;
  }
  .btnarea{
    position: fixed;
    width: 100%;
    height: 60px;
    bottom: 0;
    background: #FFF;
    // .van-button{
    //   width: 200px;
    //   height: 40px;
    //   margin: 10px auto;
    //   left: 50%;
    //   margin-left: -100px;
    // }
    .van-submit-bar__text{
      text-align: left;
      padding-left: 20px;
    }
    .van-submit-bar__price{
      font-size: unset;
    }
    // #012f40
    .van-button--danger {
      color: #fff;
      background-color: #012f40;
      border: 0.026667rem solid #012f40;
    }
  }
}
</style>


